package com.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.sys.entity.SysRole;
import com.sys.service.SysRoleService;
import com.sys.service.SysUserRoleService;
import com.bhudy.base.QueryFilter;
import com.bhudy.enums.Relation;
import com.bhudy.exception.SysException;
import com.bhudy.exception.SysExceptionCode;
import com.bhudy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author bhudy
 * @Description 角色controller
 * @Date 2019-10-30 23:09:37
 */
@RestController
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 获取sysRole的数据
     *
     * @param page     页数{1}
     * @param limit    显示数量{10}
     * @param roleName 角色名称 {admin}
     * @return sysRole的list
     */
    @GetMapping(value = "/api/v1/sysRoles")
        public ResponseResult getAll(Integer page, Integer limit, String roleName) {
        QueryFilter queryFilter = new QueryFilter();
        queryFilter.clear().and("status", Relation.NOT_EQUAL, 1);
        if (page != null && limit != null) queryFilter.limit(--page, limit);
        if (roleName != null && !roleName.equals("")) queryFilter.andLike("roleName", "%" + roleName + "%");
        return ResponseResult.printMapJson(0, sysRoleService.count(queryFilter), sysRoleService.findList(queryFilter));
    }

    @GetMapping(value = "/api/v1/sysRoleAndUsers")
    public Object sysRoleAndUsers(String userId) {
        if (userId == null) throw new SysException(SysExceptionCode.CODE_40003);
        QueryFilter queryFilter = new QueryFilter();

        Map<String, Object> resultMap = new HashMap<>();
        queryFilter.clear().and("status", Relation.NOT_EQUAL, 1);
        resultMap.put("sysRoles", sysRoleService.findList(queryFilter));
        queryFilter.clear();
        queryFilter.and("userId", userId);
        resultMap.put("sysUsers", sysUserRoleService.findList(queryFilter));

        return ResponseResult.printMapJson(0, resultMap);
    }

    @GetMapping(value = "/api/v1/sysRole")
    public ResponseResult get(Long roleId) {
        return ResponseResult.printMapJson(0, sysRoleService.get(roleId));
    }

    @PostMapping(value = "/api/v1/sysRole")
    public ResponseResult insert(@RequestBody String json) {
        return ResponseResult.printMapJson(0, sysRoleService.insert(JSONObject.parseObject(json, SysRole.class)));
    }

    @PutMapping(value = "/api/v1/sysRole")
    public ResponseResult update(@RequestBody String json) {
        return ResponseResult.printMapJson(0, sysRoleService.update(JSONObject.parseObject(json, SysRole.class)));
    }

    @DeleteMapping(value = "/api/v1/sysRoles")
    public ResponseResult delete(@RequestBody String json) {
        return ResponseResult.printMapJson(0, sysRoleService.delete(JSONObject.parseObject(json, Long[].class)));
    }

}
